package com.david.videoapp.utils

import android.text.TextUtils
import com.alibaba.fastjson.JSON
import com.david.core.event.MyEventBus
import com.david.core.normalreq.UrlHttpRequest
import com.david.core.normalreq.UrlHttpResponse
import com.david.core.utils.PreferenceUtils
import com.david.videoapp.ChannelBean
import com.david.videoapp.event.OnChannelsRefresh
import java.util.*

class LiveDataHelper {
    //黑莓电影 ：http://39.135.135.37:80/wh7f454c46tw2588615213_-1035451685/ottrrs.hl.chinamobile.com/PLTV/88888888/224/3221225743/index.m3u8?icpid=88888888&RTS=1583531260&from=1&hms_devid=683
    private var liveShows = "{" +
            "    \"size\": 3," +
            "    \"data\": [{" +
            "        \"title\": \"中央十套\"," +
            "        \"url\": \"http://183.207.249.14/PLTV/3/224/3221225550/index.m3u8\"" +
            "        }, {" +
            "         \"title\": \"中央十二套\"," +
            "            \"url\": \"http://223.110.245.172/PLTV/3/224/3221225556/index.m3u8\"" +
            "        }," +
            "        {" +
            "        \"title\": \"中央十四套\", " +
            "        \"url\": \"http://223.110.245.169/PLTV/3/224/3221227201/index.m3u8\"" +
            "        }]" +
            "}"

    var channels: ArrayList<ChannelBean>

    constructor() {
        channels = if (TextUtils.isEmpty(PreferenceUtils.getInstance().getString("channels")))
            JSON.parseArray(
                JSON.parseObject(liveShows).getString("data"),
                ChannelBean::class.java
            ) as ArrayList<ChannelBean>
        else {
            JSON.parseArray(
                PreferenceUtils.getInstance().getString("channels"),
                ChannelBean::class.java
            ) as ArrayList<ChannelBean>
        }
        addMovieItem()
    }

    companion object {
        private lateinit var instance: LiveDataHelper
        fun getDataInstance(): LiveDataHelper {
            if (!this::instance.isInitialized) {
                instance = LiveDataHelper()
            }
            return instance
        }
    }


    fun refreshData() {
        UrlHttpRequest().getRequest(
            "http://load_star.gitee.io/jsondata/liveshows",
            "",
            object : UrlHttpResponse {
                override fun onSuccess(result: String) {
                    parseResult(result)
                }

                override fun onFail(msg: String) {

                }

            })
    }

    private fun parseResult(result: String) {
        val tempChannels = JSON.parseArray(
            JSON.parseObject(result).getString("data"),
            ChannelBean::class.java
        ) as ArrayList<ChannelBean>
        var dataChanged = false
        if (channels.size != (tempChannels.size + 1)) {
            dataChanged = true
        } else
            for (index in tempChannels.indices) {
                if (tempChannels[index] != channels[index + 1]) {
                    dataChanged = true
                    break
                }
            }
        if (dataChanged) {
            channels = tempChannels

            addMovieItem()
            MyEventBus.getDefault().post(OnChannelsRefresh())
            PreferenceUtils.getInstance()
                .setString("channels", JSON.parseObject(result).getString("data"))
        }
    }

    private fun addMovieItem() {
        val staticItem = ChannelBean()
        staticItem.title = "黑莓电影"
        staticItem.url =
            "http://39.135.135.37:80/wh7f454c46tw2588615213_-1035451685/ottrrs.hl.chinamobile.com/PLTV/88888888/224/3221225743/index.m3u8?icpid=88888888&RTS=1583531260&from=1&hms_devid=683"
        channels.add(0, staticItem)
    }

}